//
//  HMComposeTextView.swift
//  WB25
//
//  Created by HM on 2017/7/8.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit

class HMComposeTextView: UITextView {
    
    
    // 重写font属性
    override var font: UIFont?{
        didSet{
            placeholderLabel.font = font
        }
    }
    
    // 占位文字
    var placeholder: String?{
        didSet{
            placeholderLabel.text = placeholder
        }
    }

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor.white
        // 添加控件
        addSubview(placeholderLabel)
        
        placeholderLabel.snp.makeConstraints { (make) in
            make.top.equalTo(8)
            make.left.equalTo(5)
            make.width.equalTo(HMSCREENW - 10)
        }
        // 通过注册系统通知监听textView文字改变
        NotificationCenter.default.addObserver(self, selector: #selector(textViewTextDidChange), name: NSNotification.Name.UITextViewTextDidChange, object: nil)
    }
    // 通过注册系统通知监听textView文字改变
    @objc private func textViewTextDidChange(){
        // 设置文字显示和隐藏
        placeholderLabel.isHidden = self.hasText
    }
    
    //MARK: - 懒加载控件
    // 占位文字
    private lazy var placeholderLabel: UILabel = {
        let lab = UILabel()
//        lab.text = "黑马25期黑马25期黑马25期黑马25期黑马25期黑马25期黑马25期黑马25期黑马25期黑马25期黑马25期黑马25期黑马25期黑马25期黑马25期黑马25期黑马25期"
//        lab.font = UIFont.systemFont(ofSize: 14)
        lab.numberOfLines = 0
        lab.textColor = UIColor.gray
        return lab
    }()
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}
